<?php
 /**
 * @package    art
 * @copyright  Copyright (c) 2013 LaZaDa SEA
 * @author     long.nguyen-thanh <long.nguyen-thanh@lazada.com
 * @date       7/24/14 4:21 PM
 */

class Zendvn_Wordpress {

    protected $_wp;

    protected static $_instance;

    protected $_postTable;

    protected $_postMetaTable;

    private function __construct(){
        global $wpdb;
        $this->_wp = $wpdb;
        $this->setPostMetaTable($this->_wp->postmeta);
        $this->setPostTable($this->_wp->posts);
    }


    public static function getInstance(){
        if(is_null(self::$_instance)){
            self::$_instance = new Zendvn_Wordpress();
        }
        return self::$_instance;
    }

    function insertToPostTable($params) {
        try{
            $this->_wp->insert( $this->getPostTable(), $params);
            $lastID =  $this->_wp->insert_id;
        }catch (Exception $e){
            throw $e;
        }
        return $lastID;
    }

    function insertToPostMetaTable($params){
        try{
            $this->_wp->insert( $this->getPostMetaTable(), $params);
        }catch (Exception $e){
            throw $e;
        }
    }

    /**
     * @param array $arrParam
     * @return mixed
     */
    function createPost($arrParam = array()) {
        $postData = array(
            'post_title' => stripslashes_deep($arrParam['title']),
            'post_content' =>stripslashes_deep($arrParam['description']),
            'post_date' => date('Y-m-d H:i:s'),
            'post_date_gmt' => date('Y-m-d H:i:s'),
            'post_author' => 1,
        );
        $lastID = $this->insertToPostTable($postData);
        if(!empty($lastID)){
            $postMeta = array(
                array(
                    'post_id' => $lastID,
                    'meta_key' => 'art_image',
                    'meta_value' => @$arrParam['image'],
                ),
                array(
                    'post_id' => $lastID,
                    'meta_key' => 'art_type',
                    'meta_value' => $arrParam['type'],
                ),
                array(
                    'post_id' => $lastID,
                    'meta_key' => 'art_bg_color',
                    'meta_value' => $arrParam['bg_color'],
                ),
                array(
                    'post_id' => $lastID,
                    'meta_key' => 'art_text_color',
                    'meta_value' => $arrParam['text_color'],
                ),
                array(
                    'post_id' => $lastID,
                    'meta_key' => 'art_text_color',
                    'meta_value' => $arrParam['text_color'],
                ),
                array(
                    'post_id' => $lastID,
                    'meta_key' => 'art_address',
                    'meta_value' => $arrParam['address'],
                ),
                array(
                    'post_id' => $lastID,
                    'meta_key' => 'art_location',
                    'meta_value' => $arrParam['location'],
                ),
                array(
                    'post_id' => $lastID,
                    'meta_key' => 'art_date',
                    'meta_value' => $arrParam['date'],
                ),
                array(
                    'post_id' => $lastID,
                    'meta_key' => 'art_time',
                    'meta_value' => $arrParam['time'],
                ),
                array(
                    'post_id' => $lastID,
                    'meta_key' => 'art_full_name',
                    'meta_value' => $arrParam['full_name'],
                ),
            );

            foreach($postMeta as $data){
                $this->insertToPostMetaTable($data);
            }

        }
        return $lastID;
    }

    /**
     * Delete WP record
     * @param $wp_id
     */
    function deleteItem($wp_id){
        $this->_wp->delete($this->getPostTable(), array( 'ID' => $wp_id ) );
        $this->_wp->delete($this->getPostMetaTable(), array( 'post_id' => $wp_id ) );
    }

    /**
     * @param mixed $postMetaTable
     */
    public function setPostMetaTable($postMetaTable) {
        $this->_postMetaTable = $postMetaTable;
    }

    /**
     * @return mixed
     */
    public function getPostMetaTable() {
        return $this->_postMetaTable;
    }

    /**
     * @param mixed $postTable
     */
    public function setPostTable($postTable) {
        $this->_postTable = $postTable;
    }

    /**
     * @return mixed
     */
    public function getPostTable() {
        return $this->_postTable;
    }

}